package it.fuesi.signit;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;


public class SettingsActivity extends Activity {

	private static final String TAG = "SignPicturesSettings";
	
	
	private EditText mSignature;
	private RadioGroup mSize;
	private Spinner mPosition;
	private SeekBar mMargin;
	private SeekBar mStroke;
	
	private SeekBar mColorR;
	private SeekBar mColorG;
	private SeekBar mColorB;
	private ImageView mColor;
	
	private Spinner mFont;
	private Spinner mFontStyle;
	
	private CurrentSettings settings;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		settings = CurrentSettings.instance(this);
		
		setContentView(R.layout.settings);
		
		mSignature = (EditText)findViewById(R.id.signature);
		mSize = (RadioGroup)findViewById(R.id.size);
		
		mPosition = (Spinner)findViewById(R.id.position);
		final String[] positions = getResources().getStringArray(R.array.settings_positions);
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, positions);
		mPosition.setAdapter(adapter);
		
		mMargin = (SeekBar)findViewById(R.id.margin);
		mMargin.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setMargin(seekBar.getProgress());
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mStroke = (SeekBar)findViewById(R.id.stroke);
		mStroke.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setStroke(seekBar.getProgress());
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mColor = (ImageView)findViewById(R.id.color);
		
		mColorR = (SeekBar)findViewById(R.id.colorR);
		mColorR.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setColorR(seekBar.getProgress());
				changeColor();
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mColorG = (SeekBar)findViewById(R.id.colorG);
		mColorG.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setColorG(seekBar.getProgress());
				changeColor();
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mColorB = (SeekBar)findViewById(R.id.colorB);
		mColorB.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				settings.setColorB(seekBar.getProgress());
				changeColor();
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		
		mFont = (Spinner)findViewById(R.id.font);
		final String[] fonts = getResources().getStringArray(R.array.settings_fonts);
		final ArrayAdapter<String> fontAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fonts);
		mFont.setAdapter(fontAdapter);
		
		mFontStyle = (Spinner)findViewById(R.id.font_style);
		final String[] fontStyles = getResources().getStringArray(R.array.settings_fonts_style);
		final ArrayAdapter<String> fontStyleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fontStyles);
		mFontStyle.setAdapter(fontStyleAdapter);
		
		setupActionBar();
		
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		savePreferences();
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		bindPreferences();
		
		changeColor();
		
		getWindow().setSoftInputMode(
			      WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}
	
	
	private void bindPreferences(){
		
		if( settings.getSignature() != null)
			mSignature.setText(settings.getSignature());
		
		mSize.check(settings.getSize());
		mPosition.setSelection(settings.getPosition());
		mMargin.setProgress(settings.getMargin());
		mStroke.setProgress(settings.getStroke());
		
		mColorR.setProgress(settings.getColorR());
		mColorG.setProgress(settings.getColorG());
		mColorB.setProgress(settings.getColorB());
		
		mFont.setSelection(settings.getTypeFace());
		mFontStyle.setSelection(settings.getTypeFaceStyle());
		
	}
	
	private void savePreferences(){
		settings.setSignature(mSignature.getText().toString());
		settings.setSize(mSize.getCheckedRadioButtonId());
		settings.setPosition(mPosition.getSelectedItemPosition());
		settings.setMargin(mMargin.getProgress());
		settings.setStroke(mStroke.getProgress());
		settings.setTypeFace(mFont.getSelectedItemPosition());
		settings.setTypeFaceStyle(mFontStyle.getSelectedItemPosition());
		
		settings.setColorR(mColorR.getProgress());
		settings.setColorG(mColorG.getProgress());
		settings.setColorB(mStroke.getProgress());
		
		settings.save();
		
	}
	
	private void changeColor(){
		int r = settings.getColorR();
		int g = settings.getColorG();
		int b = settings.getColorB();
		
		Bitmap mBitmap = Bitmap.createBitmap(96, 96,
				Bitmap.Config.ARGB_8888);
		
		Canvas c = new Canvas(mBitmap);
		
		c.drawColor(Color.argb(255, r, g, b));
		
		mColor.setImageBitmap(mBitmap);
		
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			// Show the Up button in the action bar.
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			// TODO: If Settings has multiple levels, Up should navigate up
			// that hierarchy.
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	
}
